#include "StdAfx.h"
#include "slider.h"

Slider::Slider( Qt::Orientation orientation,QWidget* parent)
: QSlider(orientation, parent)
{

	direct = orientation == Qt::Horizontal ? H : V;
	sColor = QColor(202,203,207);
	fillColor = QColor(214,182,137);
	normal = true;
	hover = false;
	press = false;
	bfill = false;
	iSlot = 0;
	sThumb = QSize(0,0);
}

Slider::~Slider()
{

}

void Slider::paintEvent(QPaintEvent *e)
{
	QStyleOptionSlider opt;
	this->initStyleOption(&opt);
	thumbRect = style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, this);

	QPainter paint(this);

	if( this->direct == H)
	{
		QRect slotRc(this->rect().x(),this->rect().height()/2-iSlot/2,
			this->rect().width(),iSlot);

		if( sbk.isNull() )
		{
			paint.setBrush(this->sColor);
			paint.drawRect(slotRc);
		}
		else
			paint.drawPixmap(slotRc, sbk);

		if(bfill)   // fill <- true
		{
			QRect fillRc(slotRc);
			fillRc.setWidth(thumbRect.x() - slotRc.x());
			if( fillSlot.isNull() )
			{
				paint.setBrush(this->fillColor);
				paint.drawRect(fillRc);
			}
			else
				paint.drawPixmap(fillRc, fillSlot);
		}

		rThumbRc.setX(thumbRect.x());
		rThumbRc.setY(this->rect().height()/2-sThumb.height()/2);
		rThumbRc.setWidth(sThumb.width());
		rThumbRc.setHeight(sThumb.height());
		// show <- true
		if(normal)
		{
			QRect real(thumbRect.x(),this->rect().height()/2-sThumb.height()/2,
				sThumb.width(),sThumb.height());
			paint.drawPixmap(real, thumbNormal);
		}
		else if(hover)
		{
			paint.drawPixmap(rThumbRc, thumbHover);
		}
		// press <- true
		else 
		{
			paint.drawPixmap(rThumbRc, thumbDown);
		}
	}
	else      // the slider is vertical
	{
		QRect slotRc(this->rect().x()+this->rect().width()/2-iSlot/2,
			this->rect().y(),iSlot,this->rect().height());

		if( sbk.isNull() )
		{
			paint.setBrush(this->sColor);
			paint.drawRect(slotRc);
		}
		else
			paint.drawPixmap(slotRc, sbk);
		if(bfill)   // fill <- true
		{
			QRect fillRc(slotRc.x(), thumbRect.top(),slotRc.width(),
				slotRc.height() - thumbRect.top());
			if( fillSlot.isNull() )
			{
				paint.setBrush(this->fillColor);
				paint.drawRect(fillRc);
			}
			else
				paint.drawPixmap(fillRc, fillSlot);
		}

		rThumbRc.setX(this->rect().width()/2 - sThumb.width()/2);
		rThumbRc.setY(thumbRect.y());
		rThumbRc.setWidth(sThumb.width());
		rThumbRc.setHeight(sThumb.height());

		if(normal)
		{
			paint.drawPixmap(rThumbRc, thumbNormal);
		}
		else if(hover)
		{
			paint.drawPixmap(rThumbRc, thumbHover);
		}
		else 
		{
			paint.drawPixmap(rThumbRc, thumbDown);
		}
	}
}

void Slider::SetImage(
					  QString thumbNormal,
					  QString thumbHover,
					  QString thumbDown,
					  QString sbk,
					  QString fill)
{
	this->sbk.load(sbk);
	this->thumbNormal.load(thumbNormal);
	this->thumbHover.load(thumbHover);
	this->thumbDown.load(thumbDown);
	this->fillSlot.load(fill);
}

void Slider::mouseMoveEvent(QMouseEvent *e)
{
	if(!press)
	{
		if( thumbRect.contains(QPoint(e->x(),e->y())))
		{
			normal = false;
			hover = true;
			press = false;
		}
		else
		{
			normal = true;
			hover = false;
			press = false;
		}
	}
	return __super::mouseMoveEvent(e);
}
void Slider::mousePressEvent(QMouseEvent *e)
{
	if( thumbRect.contains(QPoint(e->x(),e->y())) &&
		e->button() == Qt::LeftButton )
	{
		normal = false;
		hover = false;
		press = true;
	}
	return __super::mousePressEvent(e);
}
void Slider::mouseReleaseEvent(QMouseEvent *e)
{
	if( thumbRect.contains(QPoint(e->x(),e->y())))
	{
		normal = false;
		hover = true;
		press = false;
	}
	else
	{
		normal = true;
		hover = false;
		press = false;
	}
	return __super::mouseReleaseEvent(e);
}

bool Slider::event(QEvent *e)
{
	switch(e->type())
	{
	case QEvent::Leave:
		if(!press)
		{
			normal = true;
			hover = false;
			press = false;
		}
		else
		{
			normal = false;
			hover = false;
			press = true;
		}
	default:
		break;
	}
	return __super::event(e);
}
